package cc.rengu.redp.bizimpl.igas.service.impl;

import cc.rengu.redp.bizimpl.igas.domain.SEventInfo;
import cc.rengu.redp.bizimpl.igas.domain.SEventInfo;
import cc.rengu.redp.bizimpl.igas.dao.SEventInfoMapper;
import cc.rengu.redp.bizimpl.igas.domain.SUserInfo;
import cc.rengu.redp.bizimpl.igas.service.SEventInfoService;
import cc.rengu.redp.bizimpl.igas.service.SUserInfoService;
import cc.rengu.redp.bizimpl.upmp.common.BusinessContainer;
import cc.rengu.redp.bizimpl.upmp.domain.UpmpBMchntBaseInfo;
import cc.rengu.redp.bizimpl.upmp.domain.UpmpUserInfo;
import cc.rengu.redp.bizimpl.upmp.service.IUpmpBMchntBaseInfoService;
import cc.rengu.redp.bizimpl.upmp.service.IUpmpSysParamService;
import cc.rengu.redp.bizimpl.upmp.service.IUpmpUserInfoService;
import cc.rengu.redp.bizimpl.upmp.vo.UpmpUserInfoVO;
import cc.rengu.redp.bizimpl.utils.Common;
import cc.rengu.redp.bizimpl.utils.CommonUtils;
import cc.rengu.redp.common.domain.QueryRequest;
import cc.rengu.redp.common.exception.RedpException;
import cc.rengu.redp.common.utils.DateUtil;
import cc.rengu.redp.common.utils.SortUtil;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @author 刘玉亮.LL
 */
@Service
@DS("igas")
public class SEventInfoServiceImpl extends ServiceImpl<SEventInfoMapper, SEventInfo> implements SEventInfoService {
    @Autowired
    private IUpmpBMchntBaseInfoService bMchntBaseInfoService;
    @Autowired
    private IUpmpUserInfoService userInfoService;
    @Autowired
    IUpmpSysParamService upmpSysParamService;
    @Override
    public IPage<SEventInfo> findByPage(QueryRequest request, SEventInfo info, String declareDateEnd, String declareDateStart) {
        try {
            LambdaQueryWrapper<SEventInfo> queryWrapper = new LambdaQueryWrapper<>();
            if (StringUtils.isNotBlank(info.getInstId())) {
                queryWrapper.eq(SEventInfo::getInstId, info.getInstId());
            }
            if (StringUtils.isNotBlank(info.getCurrentProcUser())) {
                queryWrapper.likeRight(SEventInfo::getCurrentProcUser, info.getCurrentProcUser());
            }
            if (StringUtils.isNotBlank(info.getEventStatus())) {
                queryWrapper.likeRight(SEventInfo::getEventStatus, info.getEventStatus());
            }
            if (StringUtils.isNotBlank(declareDateEnd) && StringUtils.isNotBlank(declareDateStart)) {
                queryWrapper.ge(SEventInfo::getDeclareDate, declareDateStart);
                queryWrapper.le(SEventInfo::getDeclareDate, declareDateEnd);
            }
            queryWrapper.orderByDesc(SEventInfo::getDeclareDate);
            Page<SEventInfo> page = new Page<>();
            SortUtil.handlePageSort(request, page, true);
            Page<SEventInfo> page1 = this.page(page, queryWrapper);
            for (SEventInfo record : page1.getRecords()) {
                record.setMchntName(this.getMchntName(record.getMchntNo()));
                UpmpUserInfo user = userInfoService.getById(record.getDeclareUser());
                if (Common.isNotBlank(user)) {
                    record.setDeclareUser(user.getUserName());
                }
            }
            return page1;
        } catch (
                Exception e) {
            log.error("查询失败", e);
            return null;
        }

    }

    @Override
    public String getMchntName(String mchntNo) {

        LambdaQueryWrapper<UpmpBMchntBaseInfo> wrapper = new LambdaQueryWrapper<>();
        wrapper.ge(UpmpBMchntBaseInfo::getMchntNo, mchntNo);
        List<UpmpBMchntBaseInfo> list1 = bMchntBaseInfoService.list(wrapper);
        if (list1.size() == 0) {
            return "";
        }
        String name = list1.get(0).getMchntName().toString();
        return name;
    }

    @Override
    @Transactional
    public void update(SEventInfo entity) throws RedpException {
        // 获取当前登录用户信息
        UpmpUserInfoVO user = CommonUtils.getCurrentUser(upmpSysParamService);
        LambdaUpdateWrapper<SEventInfo> wrapper = new LambdaUpdateWrapper<>();
        wrapper.ge(SEventInfo::getInstId, entity.getInstId());
        wrapper.ge(SEventInfo::getEventId, entity.getEventId());
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        String date = sdf.format(new Date());
        SimpleDateFormat sdf1 = new SimpleDateFormat("HHmmss");
        String date1 = sdf1.format(new Date());
        entity.setDeclareUser(user.getId());
        entity.setDeclareDate(date);
        entity.setDeclareTime(date1);
        entity.setDeclarePhone(user.getMobileNo() );
        if(Common.isBlank(entity.getDeclarePhone())){
             throw new RedpException("申报人联系电话不能为空，请完善当前用户联系电话信息");
        }
        this.saveOrUpdate(entity,wrapper);
    }
    @Override
    public Map<String,Object> detail(String id, String instId){
        HashMap<String, Object> result = new HashMap<>();
        LambdaQueryWrapper<SEventInfo> wrapper = new LambdaQueryWrapper<>();
        wrapper.ge(SEventInfo::getInstId, instId);
        wrapper.ge(SEventInfo::getEventId, id);
        SEventInfo entity = this.getOne(wrapper);
        if (entity.getEventType().equals("02")) {
            List<Map<String,String>> list=new  ArrayList<>();
            list = this.baseMapper.getNoAmt(entity.getOrigTransSeqNum());
            String str=" , ";
            if (list.size() != 0) {
                String no = list.get(0).get("PAYER_ACCT_NO").toString();
                String amt = list.get(0).get("TRANS_AMT").toString();
                str = no + "," + amt;
            }
            String str1[] = str.split(",");
            entity.setOrigPayAcctNo(str1[0]);
            entity.setOrigTransAmt(str1[1]);
        }
        String declareUser=entity.getDeclareUser();
        UpmpUserInfo user = userInfoService.getById(entity.getDeclareUser());
        if(Common.isNotBlank(user)){
            entity.setDeclareUser(user.getUserName());
        }
        result.put("entity", entity);
        result.put("voucherImagePath1", entity.getVoucherImagePath1());
        result.put("voucherImagePath2", entity.getVoucherImagePath2());
        result.put("voucherImagePath3", entity.getVoucherImagePath3());
        result.put("status", entity.getEventType());
        return result;
    }
}
